package com.manolo.framework.engine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.manolo.framework.deck.Card;
import com.manolo.framework.deck.Deck;
import com.manolo.framework.deck.SUIT;

public class Engine {
	// this.

	public static int getTheHandWinner(Map<Integer, Card> map,
			int numberOfRounds) {
		Set set = map.entrySet();
		Iterator i = set.iterator();

		Card winnerCard = new Card();
		Integer winnerPlayer = 1;
		while (i.hasNext()) {
			Map.Entry me = (Map.Entry) i.next();
			// me.getKey() + " : " + me.getValue();
			if (winnerCard == null) {
				winnerCard = (Card) me.getValue();
			}

			if (numberOfRounds == 3) {
				if ((winnerCard.getForce() == ((Card) me.getValue()).getForce()) && ((winnerCard.getSuit().getIdSuit()) < ((Card) me.getValue()).getSuit().getIdSuit())) {
					winnerPlayer = (Integer) me.getKey();
					winnerCard = (Card) me.getValue();
				} else if (winnerCard.getForce() < ((Card) me.getValue())
						.getForce()) {
					winnerPlayer = (Integer) me.getKey();
					winnerCard = (Card) me.getValue();
				}
			} else if (winnerCard.getForce() == ((Card) me.getValue())
					.getForce()) {
				winnerPlayer = 5;
			} else if (winnerCard.getForce() < ((Card) me.getValue())
					.getForce()) {
				winnerPlayer = (Integer) me.getKey();
				winnerCard = (Card) me.getValue();
			}
		}
		return winnerPlayer;
	}

	public static void main(String[] args) {
		Deck deck = new Deck(1, 3, 4);
		// Shuffle the Deck
		deck.shuffleDeck();
		// Cut the deck at the position 2
		deck.cutDeck(22);

		ArrayList<List<Card>> allHands = deck.distributeCards();

		Map<Integer, Card> map = new HashMap<Integer, Card>();
		map.put(1, allHands.get(0).get(0));
		System.out.println(allHands.get(0).get(0));
		map.put(2, allHands.get(1).get(0));
		System.out.println(allHands.get(1).get(0));
		map.put(3, allHands.get(2).get(0));
		System.out.println(allHands.get(2).get(0));
		map.put(4, allHands.get(3).get(0));
		System.out.println(allHands.get(3).get(0));
		System.out.println(Engine.getTheHandWinner(map,1));

	}

}
